<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">服装管理</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">新建服装</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="服装品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服装编号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="服装型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服装识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="服装库存">
            <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
              <el-option label="2000" value="1"></el-option>
              <el-option label="2500" value="2"></el-option>
              <el-option label="3000" value="3"></el-option>
              <el-option label="3500" value="4"></el-option>
              <el-option label="4000" value="5"></el-option>
              <el-option label="4500" value="6"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服装类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.type">
              <el-option label="短袖" value="10"></el-option>
              <el-option label="牛仔裤" value="20"></el-option>
              <el-option label="连衣裙" value="30"></el-option>
              <el-option label="羽绒服" value="40"></el-option>
              <el-option label="西装" value="50"></el-option>
              <el-option label="外套" value="60"></el-option>
              <el-option label="运动鞋" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="服装颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.buyTime"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出库时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveVehicleForm.regTime"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服装材质类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option label="丝绸" value="10"></el-option>
              <el-option label="亚麻" value="20"></el-option>
              <el-option label="纯棉" value="30"></el-option>
              <el-option label="羊毛" value="40"></el-option>
              <el-option label="牛仔" value="50"></el-option>
              <el-option label="冰丝" value="60"></el-option>
              <el-option label="羽绒" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="服装品牌">
        <el-input placeholder="请输入内容" style="width:220px;"
                  v-model="searchVehicleForm.brand" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item label="服装编号" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;"
                  v-model="searchVehicleForm.license" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="服装品牌" align="center"/>
      <el-table-column prop="license" label="服装编号" align="center"/>
      <el-table-column prop="code" label="服装识别码" align="center"/>
      <el-table-column prop="type" label="服装类型" align="center"
                       :formatter="vehicleTypeFormatter"/>

      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="出库时间" align="center"/>
      <el-table-column prop="batteryType" label="服装材质" align="center"
                       :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="服装状态" align="center"
                       :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增服装');
//定义对象用来保存车辆搜索条件 品牌 车牌号
const searchVehicleForm = ref({brand:'',license:''});
//定义数组用来保存车辆表格数据
const vehicleArr = ref([]);
//定义加载车辆的方法
const loadVehicle = ()=>{
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+'/v1/vehicle/select?'+data).then((response)=>{
    if(response.data.code==2000){
      vehicleArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//onMounted() VUE的生命周期钩子 在DOM渲染完毕后立即执行
onMounted(()=>{
  loadVehicle();
})
//定义搜索卡片重置按钮的功能 清空值+重新加载车辆
const resetSearch = ()=>{
  searchVehicleForm.value = {};
  loadVehicle();
}

//对车辆状态显示内容进行文字转换
const vehicleStatusFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '空闲';
  }else if(cellValue == 2){
    cellValue = '占用';
  }
  return cellValue;
}

//对车辆类型显示内容进行文字转换
const vehicleTypeFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = '短袖';
  }else if(cellValue == 20){
    cellValue = '牛仔裤';
  }else if(cellValue == 30){
    cellValue = '连衣裙';
  }else if(cellValue == 40){
    cellValue = '羽绒服';
  }else if(cellValue == 50){
    cellValue = '西装';
  }else if(cellValue == 60){
    cellValue = '外套';
}else if(cellValue == 70){
  cellValue = '运动鞋';
  }
  return cellValue;
}

//对电池类型显示内容进行文字转换
const batteryTypeFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = '丝绸';
  }else if(cellValue == 20){
    cellValue = '亚麻';
  }else if(cellValue == 30){
    cellValue = '纯棉';
  }else if(cellValue == 40){
    cellValue = '羊毛';
  }else if(cellValue == 50){
    cellValue = '牛仔';
  }else if(cellValue == 60){
    cellValue = '冰丝';
  }else if(cellValue == 70){
    cellValue = '羽绒';
  }
  return cellValue;
}
//定义对象用来收集车辆弹窗表单的所有数据
const saveVehicleForm = ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:'',
})
//定义保存车辆的方法
const saveVehicle = ()=>{
  //把收集到的表单数据转为查询字符串格式
  let data = qs.stringify(saveVehicleForm.value);
  //给后端发请求
  axios.post(BASE_URL+'/v1/vehicle/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功!');
      saveVehicleForm.value={};
      dialogVisible.value=false;
      loadVehicle();
    }else{
      ElMessage.error('服装编号或者服装识别码不允许重复');
    }
  })
}

//处理保存车辆弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本弹窗吗?')){
    dialogVisible.value = false;
  }
}
const editVehicle=(id)=>{
  dialogVisible.value=true;
  dialogTitle.value='编辑服装';
  axios.get(BASE_URL+'/v1/vehicle/select?id='+id).then((response)=>{
    if (response.data.code==2000){
      saveVehicleForm.value=response.data.data[0];
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

const deleteVehicle=(id)=> {
  if (confirm('您确定要删除此服装吗？')) {
    axios.post(BASE_URL + '/v1/vehicle/delete/' + id).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功！');
        loadVehicle();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>